/*
This file creates the tables and figures in the Online Appendix for Sandvik, Sauoma, Seegert, Stanton (2020)


Inputs
	 Data 
		1) DataforAnalysis.dta merged in file MergeData.do
		2) manager Demographics Data.dta additional data given by the company on managers
		3) survey1.dta data about who was randomly paired and rotated
		4) 1810-survey-roster.csv used in appendix figure 1 and 4
		5) Helping Survey_October 19, 2018_13.20.csv used in appendix figure 1
Outputs
	Tables
		1) Table Appendix 1 Pre-Experiment and Post-Experiment Survey Responses for Treatment-Eligible Agents 
		2) Table Appendix 2 Balance Across Managers
		3) Table Appendix 3 Analysis of Other Sales Measures in the Intervention and Post-Intervention Periods
		4) Table Appendix 4 Do Agents Give-up After Winning or Losing a Prize
		5) Table Appendix 5 Difference-in-Differences Estimates of Treatment Effects at the Manager Level
		6) Table Appendix 6 Evidence of Quality Changes During the Intervention Period
		7) Table Appendix 7 Two-Way Cluster Difference-in-Differences Estimates of Log Revenue-per-Call Changes During 
			the Post-Intervention Period
		8) Table Appendix 8 Estimates of Persistence of Log RPC Gains Over Different Intervals in the Post-Intervention Period 
		9) Table Appendix 9 Estimates of Log Revenue-per-Call Changes During the Post-Intervention period weighting by the 
			inverse number of weeks an agents is in the sample
		10) Table Appendix 10 Agent Turnover at Different Horizons
		11) Table Appendix 11 Treatment Effect heterogeneity on Log Revenue-per-Call by Partner and Agent Performance
		12) Table Appendix 12 Treatment Effect Heterogeneity on Total Calls by Partner and Agent Performance
		13) Table Appendix 13 Example Worksheet Responses Organized by Word Prevalence [not produced in this file]
		14) Table Appendix 14 Estimates of Log Revenue-per-Call Treatment Effects During the Post-Intervention Period 
		15) Table Appendix 15 Estimates of Log Revenue-per-Call Treatment Effect Heterogeneity by Rotation and Baseline Connections
		16) Table Appendix 16 Estimates of Treatment Effects on Log Revenue-per-Call During the Post-Intervention Period for Agents
			in the Top 75\% 
		17  Figure Appendix 1 Perceived and Actual Differences Between Individual and the Top Sales Agents
		18) Figure Appendix 2 Revenue-per-call Over an Extended Post-intervention Period
		19) Figure Appendix 3 Average Revene-per-Call in the Intervention and Post-Intervention Periods by High-Performer 
			Partner Assigment
		20) Figure Appendix 4 Agents' Reported Estimates of Perceived Treatment Effects After Help from High-Performers
        21) Figure Appendix 5 Knowledge Transfer Region       
		
 
*/

clear
set more off
clear all
 
 
*set paths 
	cd   
	global datapath   
	global tablepath  
	global figurepath   
    global logpath
 
 
	local managOverhead = 250000 // Overhead to allocate toward implementation cost.
	use "$datapath\DataforAnalysis.dta", clear
	cap log close
	log using "$logpath\produceAppendixTables.smcl", replace

*Save tests 
	matrix pval = J(4,11,0)	
	matrix individuals = J(4,6,0)
	matrix ind_treat = J(5,6,0)
	matrix ind_treat_persist = J(5,6,0)
	matrix man_treat = J(5,6,0)
	matrix man_treat_persist = J(5,6,0)
	matrix obs_treat = J(5,6,0)
	matrix obs_treat_persist = J(5,6,0)
*------------------------------------------------------------------------------*
*           Table Samples 
*------------------------------------------------------------------------------*

*Table 2 baseline analysis
	local baselinesample1   "if Week > -4 & Week < 5 & Treatment !=5"
	local baselinesample2   "if Week > -4 & Week < 5 & Treatment !=1"
	local baselinesample3   "if Week > -4 & Week < 5"
	local baselinesample4   "if Week > -4 & Week < 5"
	
*Table 3 persistence
	local persistencesample1   "if (Week >= -3 & Week <= 0 & Treatment !=5) | (Week >= 5 & Week <= 24 & Treatment !=5)  "
	local persistencesample2   "if (Week >= -3 & Week <= 0 & Treatment !=1) | (Week >= 5 & Week <= 24 & Treatment !=1) "
	local persistencesample3   "if (Week >= -3 & Week <= 0) | (Week >= 5 & Week <= 24) "
	local persistencesample4   "if (Week >= -3 & Week <= 0 & balancesample ==1) | (Week >= 5 & Week <= 24 & balancesample ==1)    "
	local persistencesample5   "if (Week >= -3 & Week <= 0) | (Week >= 5 & Week <= 24) "

	
*Table Check_ persistence
	bysort ID: egen avgTenure = mean(exp(tenure))
	bysort ID: gen toUsePerson_temp = _n==1
	sum avgTenure if toUsePerson_temp==1 & Treatment !=5, d
	gen nonJoinerSample = avgTenure>=r(p25)
	sum tenure if toUsePerson_temp==1 & Treatment !=5, d
	local checkpersistsample1   "if nonJoinerSample & ((Week >= -3 & Week <= 0 & Treatment !=5) | (Week >= 5 & Week <= 24 & Treatment !=5))  "
	local checkpersistsample2   "if nonJoinerSample & ((Week >= -3 & Week <= 0 & Treatment !=1) | (Week >= 5 & Week <= 24 & Treatment !=1)) "
	local checkpersistsample3   "if nonJoinerSample & ((Week >= -3 & Week <= 0) | (Week >= 5 & Week <= 24)) "
	local checkpersistsample4   "if nonJoinerSample & ((Week >= -3 & Week <= 0 & balancesample ==1) | (Week >= 5 & Week <= 24 & balancesample ==1))    "
	local checkpersistsample5   "if nonJoinerSample & ((Week >= -3 & Week <= 0) | (Week >= 5 & Week <= 24)) "
	
*Table 5 high and non high person subsamples
	local highsample1 `baselinesample1'
	local highsample2 "if Week > -4 & Week < 5 & Treatment !=5 & highperformer == 0"
	local highsample3 "if Week > -4 & Week < 5 & Treatment !=5 & highperformer == 1"
	local highsample4 "if ((Week >= -3 & Week <= 0  & Treatment !=5 & highperformer == 0) | (Week >= 5 & Week <= 24  & Treatment !=5 & highperformer == 0)) | ((Week >= -3 & Week <= 0 & Treatment !=5 & highperformer == 1 ) | (Week >= 5 & Week <= 24 & Treatment !=5 & highperformer == 1 ) )"
	local highsample5 "if (Week >= -3 & Week <= 0  & Treatment !=5 & highperformer == 0) | (Week >= 5 & Week <= 24  & Treatment !=5 & highperformer == 0) "
	local highsample6 "if (Week >= -3 & Week <= 0 & Treatment !=5 & highperformer == 1 ) | (Week >= 5 & Week <= 24 & Treatment !=5 & highperformer == 1 ) "

	
*Table A.5 Manager	
	local baselinesample5   "if Week > -4 & Week < 5 & uniqueManagerWeek ==1"
	local persistencesample6   "if (Week >= -3 & Week <= 0 & uniqueManagerWeek ==1) | (Week >= 5 & Week <= 24 & uniqueManagerWeek ==1) "
	

*------------------------------------------------------------------------------*
** Table A.1  Pre-Experiment and Post-Experiment Survey Responses 
**          for Treatment-Eligible Agents 
*------------------------------------------------------------------------------*
preserve
	gen counter = _n
	replace counter = . if counter > 11
 	gen column1 = 1
	gen column2 = (Treatment == 1)
	gen column3 = (Treatment == 2)
	gen column4 = (Treatment == 3)
	gen column5 = 	(Treatment == 4)

	forvalues i = 1(1)5 {
		gen y`i' = .	
			
		sum HowConnectedDoYouFeel            if column`i' == 1, d 
			replace y`i' = round(r(mean),.1) if counter == 1
		sum HowManyWorkRelatedInteractions   if column`i' == 1, d 
			replace y`i' = round(r(mean),.1) if counter == 4
		sum HowBeneficialAreTheseInteract    if column`i' == 1, d 
			replace y`i' = round(r(mean),.1) if counter == 7
		sum WhatDollarValueWouldYou          if column1   == 1, d 
			replace y1 = round(r(mean),.1)   if counter == 10
	}  

	forvalues i = 1(1)11 {
		gen e`i' = (counter==`i')
	}

*label rows
	label var e1 "\textit{On a scale of 1-5, how connected to you}"
	label var e2 "\textit{feel to others within the firm?}" 
	label var e3 " "
	label var e4 "\textit{How many work-related interactions do you}"
	label var e5 "\textit{initiate in an average week?}" 
	label var e6 " " 
	label var e7 "\textit{On a scale of 1-5, how beneficial are these}" 
	label var e8 "\textit{interactions to you personally?}"
	label var e9 " " 
	label var e10 "\textit{What dollar value would you be willing to}" 
	label var e11 "\textit{spend on the proposed incentives?}"

	local outreg_opt = "replace"
	forvalues r=1(1)5 {
	qui reg y`r' e1-e11, nocons noheader
	outreg2 e1-e11 using "$tablepath/Tableapp1a.tex", nose label noaster tex /*dec(3)*/ ctitle(`ctitle`r'') `outreg_opt'	 
	local outreg_opt = "append" 
	}

	drop y1-y5 e1-e11 column1-column5 counter
	
*** Table A.1 Panel B: Post-Experiment Survey
	gen counter = _n
	replace counter = . if counter > 13
 *generate columns (stored in y`i')
	gen column1 = 1
	gen column2 = (Treatment == 1)
	gen column3 = (Treatment == 2)
	gen column4 = (Treatment == 3)
	gen column5 = (Treatment == 4)
		
	forvalues i = 1(1)5 {
		gen y`i' = .	
			
		sum IWasAwareOfTheExperimentThat       if column`i' == 1, d 
			replace y`i' = round(r(mean),.001) if counter == 1
	}
	
	forvalues i = 4(1)5 {   	
		sum WeTurnedInACompletedWorksheet      if column`i' == 1, d 
			replace y`i' = round(r(mean),.001) if counter == 4
		sum ISpentBlankMinutesWithMy           if column`i' == 1, d 
			replace y`i' = round(r(mean),.1)   if counter == 7
		sum TheseInteractionsWithMyPartner     if column`i' == 1, d 
			replace y`i' = round(r(mean),.001) if counter == 10
	}
	
	forvalues i = 1(1)5 {
		sum Survey                             if column`i' == 1, d
			replace y`i' = r(N)                if counter == 13	
	}  
	
*create rows
	forvalues i = 1(1)13 {
		gen e`i' = (counter==`i')
	}
	
*label rows
	label var e1 "\textit{I was aware of the experiment}"
	label var e2 "\textit{that took place this past month.}" 
	label var e3 " "
	label var e4 "\textit{We turned in a completed}"
	label var e5 "\textit{worksheet each week.}" 
	label var e6 " " 
	label var e7 "\textit{I spent [ ] minutes with my}" 
	label var e8 "\textit{partner on the worksheet.}"
	label var e9 " " 
	label var e10 "\textit{These interactions with my}" 
	label var e11 "\textit{partner were beneficial.}"
	label var e12 " " 
	label var e13 "N$_A$ (Agents)"
	
	local outreg_opt = "replace"
	forvalues r=1(1)5 {
	qui reg y`r' e1-e13, nocons noheader
	outreg2 e1-e13 using "$tablepath/TableA1.tex", nose label noaster tex /*dec(3)*/ ctitle(`ctitle`r'') `outreg_opt'	 
	local outreg_opt = "append" 
	}
	
	drop y1-y5 e1-e13 column1-column5 counter

restore 

*------------------------------------------------------------------------------*
** Table A.2  Balance Across Managers
**           
*------------------------------------------------------------------------------*

preserve
	*determine teammates  
		gen  period = ( Week < 5 & Week > 0)
		bysort manager ID  period: gen uniqueAgentNum = 1 if _n ==1  
		sum uniqueAgentNum if  period==1
		bysort manager: egen totalAgents = total(uniqueAgentNum) if  period ==1
		sum totalAgents  [fw = uniqueAgentNum]
		sum totalAgents  [fw = uniqueAgentNum] if Treatment ==1 , d   
		sum totalAgents [fw = uniqueAgentNum] if Treatment ==2   , d  
		sum totalAgents [fw = uniqueAgentNum] if Treatment ==3   , d 
		sum totalAgents [fw = uniqueAgentNum] if Treatment ==4, d
		sum totalAgents [fw = uniqueAgentNum] if Treatment ==5, d
		bysort manager  period: gen uniqueManager2 = 1 if _n ==1 
		reg totalAgents i.Treatment if Treatment !=5 & uniqueManager2 ==1
		test   2.Treatment = 3.Treatment = 4.Treatment
	
	*eligible agents
		bysort location brand Treatment period ID: gen numPotential = 1 if _n ==1  	
		sum numPotential [fw = uniqueAgentNum]
		sum numPotential  [fw = uniqueAgentNum] if Treatment ==1 , d   
		sum numPotential [fw = uniqueAgentNum] if Treatment ==2   , d  
		sum numPotential [fw = uniqueAgentNum] if Treatment ==3   , d 
		sum numPotential [fw = uniqueAgentNum] if Treatment ==4, d
		sum numPotential [fw = uniqueAgentNum] if Treatment ==5, d

	use "$datapath\manager Demographics Data.dta", clear

	gen counter = _n
	replace counter = . if counter > 17
	 
	gen y1 = .
	gen y2 = .
	gen y3 = .
	gen y4 = .
	gen y5 = .	
	gen y6 = . 
	gen y7 = .

	sum manager_Age if manager_Treatment >= 1 & manager_Treatment <= 5
	replace y1 = r(mean) in 4
	replace y1 = r(sd)   in 5
	sum manager_Age if manager_Treatment == 3
	replace y2 = r(mean) in 4
	replace y2 = r(sd)   in 5
	sum manager_Age if manager_Treatment == 2
	replace y3 = r(mean) in 4
	replace y3 = r(sd)   in 5
	sum manager_Age if manager_Treatment == 4
	replace y4 = r(mean) in 4
	replace y4 = r(sd)   in 5
	sum manager_Age if manager_Treatment == 1
	replace y5 = r(mean) in 4
	replace y5 = r(sd)   in 5
	sum manager_Age if manager_Treatment == 5
	replace y6 = r(mean) in 4
	replace y6 = r(sd)   in 5

	regress manager_Age manager_T1 manager_T2 manager_T3 manager_T4 if manager_Treatment >= 1 & manager_Treatment <= 4, noconstant
	test manager_T1 = manager_T2 = manager_T3 = manager_T4
	replace y7 = r(p) in 4

	sum manager_Tenure if manager_Treatment >= 1 & manager_Treatment <= 5
	replace y1 = r(mean) in 7
	replace y1 = r(sd)   in 8
	sum manager_Tenure if manager_Treatment == 3
	replace y2 = r(mean) in 7
	replace y2 = r(sd)   in 8
	sum manager_Tenure if manager_Treatment == 2
	replace y3 = r(mean) in 7
	replace y3 = r(sd)   in 8
	sum manager_Tenure if manager_Treatment == 4
	replace y4 = r(mean) in 7
	replace y4 = r(sd)   in 8
	sum manager_Tenure if manager_Treatment == 1
	replace y5 = r(mean) in 7
	replace y5 = r(sd)   in 8
	sum manager_Tenure if manager_Treatment == 5
	replace y6 = r(mean) in 7
	replace y6 = r(sd)   in 8

	regress manager_Tenure manager_T1 manager_T2 manager_T3 manager_T4 if manager_Treatment >= 1 & manager_Treatment <= 4, noconstant
	test manager_T1 = manager_T2 = manager_T3 = manager_T4
	replace y7 = r(p) in 7

	sum manager_Female if manager_Treatment >= 1 & manager_Treatment <= 5
	replace y1 = r(mean) in 10
	replace y1 = r(sd)   in 11
	sum manager_Female if manager_Treatment == 3
	replace y2 = r(mean) in 10
	replace y2 = r(sd)   in 11
	sum manager_Female if manager_Treatment == 2
	replace y3 = r(mean) in 10
	replace y3 = r(sd)   in 11
	sum manager_Female if manager_Treatment == 4
	replace y4 = r(mean) in 10
	replace y4 = r(sd)   in 11
	sum manager_Female if manager_Treatment == 1
	replace y5 = r(mean) in 10
	replace y5 = r(sd)   in 11
	sum manager_Female if manager_Treatment == 5
	replace y6 = r(mean) in 10
	replace y6 = r(sd)   in 11

	regress manager_Female manager_T1 manager_T2 manager_T3 manager_T4 if manager_Treatment >= 1 & manager_Treatment <= 4, noconstant
	test manager_T1 = manager_T2 = manager_T3 = manager_T4
	replace y7 = r(p) in 10

	sum manager_RPC if manager_Treatment >= 1 & manager_Treatment <= 5
	replace y1 = r(mean) in 14
	replace y1 = r(sd)   in 15
	sum manager_RPC if manager_Treatment == 3
	replace y2 = r(mean) in 14
	replace y2 = r(sd)   in 15
	sum manager_RPC if manager_Treatment == 2
	replace y3 = r(mean) in 14
	replace y3 = r(sd)   in 15
	sum manager_RPC if manager_Treatment == 4
	replace y4 = r(mean) in 14
	replace y4 = r(sd)   in 15
	sum manager_RPC if manager_Treatment == 1
	replace y5 = r(mean) in 14
	replace y5 = r(sd)   in 15
	sum manager_RPC if manager_Treatment == 5
	replace y6 = r(mean) in 14
	replace y6 = r(sd)   in 15

	regress manager_RPC manager_T1 manager_T2 manager_T3 manager_T4 if manager_Treatment >= 1 & manager_Treatment <= 4, noconstant
	test manager_T1 = manager_T2 = manager_T3 = manager_T4
	replace y7 = r(p) in 14

	count if manager_Treatment != .
	replace y1 = r(N) in 17
	count if manager_Treatment == 3
	replace y2 = r(N) in 17
	count if manager_Treatment == 2
	replace y3 = r(N) in 17
	count if manager_Treatment == 4
	replace y4 = r(N) in 17
	count if manager_Treatment == 1
	replace y5 = r(N) in 17
	count if manager_Treatment == 5
	replace y6 = r(N) in 17	

	gen e1 = 0
	gen e2 = 0
	gen e3 = 0
	gen e4 = 0
	gen e5 = 0
	gen e6 = 0
	gen e7 = 0
	gen e8 = 0
	gen e9 = 0
	gen e10 = 0
	gen e11 = 0
	gen e12 = 0
	gen e13 = 0
	gen e14 = 0
	gen e15 = 0
	gen e16 = 0
	gen e17 = 0

	replace e1 = 1 if counter == 1
	replace e2 = 1 if counter == 2
	replace e3 = 1 if counter == 3
	replace e4 = 1 if counter == 4
	replace e5 = 1 if counter == 5
	replace e6 = 1 if counter == 6
	replace e7 = 1 if counter == 7
	replace e8 = 1 if counter == 8
	replace e9 = 1 if counter == 9
	replace e10 = 1 if counter == 10
	replace e11 = 1 if counter == 11
	replace e12 = 1 if counter == 12
	replace e13 = 1 if counter == 13
	replace e14 = 1 if counter == 14
	replace e15 = 1 if counter == 15
	replace e16 = 1 if counter == 16
	replace e17 = 1 if counter == 17

	label var e1  "Panel C: All Managers"
	label var e2  " "
	label var e3 "Manager Age (yrs.)"
	label var e4 "\hspace{4mm} Mean"
	label var e5 "\hspace{4mm} Std Dev."	
	label var e6 "Manger Tenure (yrs.)"
	label var e7 "\hspace{4mm} Mean"
	label var e8 "\hspace{4mm} Std Dev."	
	label var e9 "Manager Female"
	label var e10 "\hspace{4mm} Mean"
	label var e11 "\hspace{4mm} Std Dev."
	label var e12 "Average log(RPC) of Workers"
	label var e13 "on Manager's Team"
	label var e14 "\hspace{4mm} Mean"
	label var e15 "\hspace{4mm} Std Dev."
	label var e16 ""
	label var e17 "N Managers"
	
	local outreg_opt = "replace"
	forvalues r=1(1)7 {
	  qui reg y`r' e1-e17, nocons noheader
		outreg2 e1-e17 using "$tablepath\TableA2.tex", nose label noaster tex  ctitle(`ctitle`r'')   `outreg_opt'
	local outreg_opt = "append"
	}



restore 
 
*------------------------------------------------------------------------------*
** Table A.3 Analysis of Other Sales Measures 
**            in the Intervention and Post-Intervention Periods          
*------------------------------------------------------------------------------*

*generate log commissions 
	gen log_commission = log(commission)

*Revenue per hour
*Treatment period internal control 
	reghdfe RPH  pairincentive structuredmeeting combined internalcontrol     `baselinesample3' , absorb(manager Week) vce(cluster manager)	
	est store A
	
*Post period internal control 
	reghdfe RPH      pairincentivepost structuredmeetingpost combinedpost   internalcontrolpost   `persistencesample3' , absorb(manager Week)   vce(cluster manager)
	est store B	
	
*Log revenue per hour
*Treatment period internal control 
	reghdfe lrph   pairincentive structuredmeeting combined internalcontrol      `baselinesample3' , absorb(manager Week) vce(cluster manager)	
	est store C
	
*Post period internal control 
	reghdfe lrph     pairincentivepost structuredmeetingpost combinedpost   internalcontrolpost    `persistencesample3' , absorb(manager Week)   vce(cluster manager)
	est store D
	
*Revenue per call
*Treatment period internal control 
	reghdfe RPC   pairincentive structuredmeeting combined internalcontrol     `baselinesample3' , absorb(manager Week) vce(cluster manager)	
	est store E
	
*Post period internal control 
	reghdfe RPC      pairincentivepost structuredmeetingpost combinedpost   internalcontrolpost   `persistencesample3' , absorb(manager Week)   vce(cluster manager)
	est store F

 
estout1 A B C D E F   using "$tablepath\TableA3.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(structuredmeeting  pairincentive  combined internalcontrol structuredmeetingpost pairincentivepost  combinedpost  internalcontrolpost) 


*------------------------------------------------------------------------------*
** Table A.4   Do Agents Give-up After Winning or Losing a Prize
**           
*------------------------------------------------------------------------------*
 *generate winner variables
	replace Winner = 0 if Winner == .
	sort ID Week
	gen WonLastWeek = 0
		replace WonLastWeek = 1 if Winner[_n-1] == 1 & ID == ID[_n-1] & Week == (Week[_n-1] + 1)

*Pair-incentives 
	reghdfe lrpc WonLastWeek  period1_4 if (Treatment== 2 &  Week <5 & Week > -4)                , absorb(manager Week) vce(cluster manager)	
		est store A
		
*Combined		
	reghdfe lrpc WonLastWeek  period1_4 if (Treatment== 4 &  Week <5 & Week > -4)                     , absorb(manager Week) vce(cluster manager)	
		est store B

*Pair-incentives and combined
	reghdfe lrpc WonLastWeek  period1_4 if ((Treatment== 4 &  Week <5 & Week > -4)   | (Treatment== 2 &  Week <5 & Week > -4)), absorb(manager Week) vce(cluster manager)	
		est store C		
		
	estout1 A B C using "$tablepath\TableA4.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(WonLastWeek)	

*------------------------------------------------------------------------------*
** Table A.5  Difference-in-Differences Estimates of Treatment Effects 
**            at the Manager Level
*------------------------------------------------------------------------------*

*Baseline
	reghdfe lrpc_manager    internalcontrol pairincentive structuredmeeting combined   `baselinesample5', absorb(manager Week) vce(cluster manager)	
	est store A
	
*Baseline
	reghdfe revenue_manager    internalcontrol pairincentive structuredmeeting combined   `baselinesample5', absorb(manager Week) vce(cluster manager)	
	est store B
	
	foreach var in internalcontrol pairincentive structuredmeeting combined { 
		rename `var' `var'_temp 
		rename `var'post `var'
	}
	
*Persistence	
	reghdfe lrpc_manager     internalcontrol pairincentive structuredmeeting combined   `persistencesample6', absorb(manager Week) vce(cluster manager)	
	est store C
	
*Revenue at the manager level	
	reghdfe revenue_manager   internalcontrol pairincentive structuredmeeting combined    `persistencesample6', absorb(manager Week) vce(cluster manager)	
	est store D
 
	foreach var in internalcontrol pairincentive structuredmeeting combined { 
		rename `var' `var'post 
		rename `var'_temp `var'
	}
	
*WEIGHTED BY NUMBER OF AGENTS (see references in text for motivatoin )	

bysort manager Week: gen w_agent = _N

*Baseline
	reghdfe lrpc_manager    internalcontrol pairincentive structuredmeeting combined  [fw = w_agent] `baselinesample5', absorb(manager Week) vce(cluster manager)	
	est store E
	
*Baseline
	reghdfe revenue_manager    internalcontrol pairincentive structuredmeeting combined [fw = w_agent]  `baselinesample5', absorb(manager Week) vce(cluster manager)	
	est store F
	
	foreach var in internalcontrol pairincentive structuredmeeting combined { 
		rename `var' `var'_temp 
		rename `var'post `var'
	}
	
*Persistence	
	reghdfe lrpc_manager     internalcontrol pairincentive structuredmeeting combined   [fw = w_agent] `persistencesample6', absorb(manager Week) vce(cluster manager)	
	est store G
	
*Revenue at the manager level
	reghdfe revenue_manager   internalcontrol pairincentive structuredmeeting combined  [fw = w_agent]  `persistencesample6', absorb(manager Week) vce(cluster manager)	
	est store H
	
		
	foreach var in internalcontrol pairincentive structuredmeeting combined { 
		rename `var' `var'post 
		rename `var'_temp `var'
	}
 
	estout1 A B C D E F G H using  "$tablepath\TableA5.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace  keep(structuredmeeting  pairincentive combined internalcontrol) 
	

 
*------------------------------------------------------------------------------*
** Table A.6  Evidence of Quality Changes During the Intervention Period
**           
*------------------------------------------------------------------------------*
 
	* Week 1 ends Jul 22, so it is the beginning of a pay period.  
	* So adding an odd number makes week 1 and other odd weeks the beginning of pay period.
		gen payperiod = Week+11-mod(Week+11,2)
		gen rpcquintile = .
		gen rphquintile = .

		forvalues i=1(1)6 {
			sum payperiod, d
			forvalues t =`=r(min)' (2) `=r(max)' {
				capture drop rpc_quintile rph_quintile
				cap xtile rpc_quintile=RPC if brand ==`i' & payperiod==`t', n(5)
				cap replace rpcquintile = rpc_quintile if brand ==`i' & payperiod==`t'
				cap xtile rph_quintile=RPH if brand ==`i' & payperiod==`t', n(5)
				cap replace rphquintile = rph_quintile if brand ==`i' & payperiod==`t'
			}
		}

	* Get brand-specific interactions with rph/rpc
		forvalues i = 1(1)6 {
			gen rphquintile_brand`i' = rpcquintile*(brand==`i')
			gen rpcquintile_brand`i' = rphquintile*(brand==`i')
		}
		gen Quality = log(commission)-log(Revenue)

*Intervention period
	reghdfe Quality  rphquintile_brand* rpcquintile_brand* pairincentive structuredmeeting combined `baselinesample1' , absorb(manager Week) vce(cluster manager)
	est store A
	

	 estout1 A   using "$tablepath\TableA6.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(structuredmeeting  pairincentive  combined ) 
 
*------------------------------------------------------------------------------*
** Table A.7   Two-Way Cluster Difference-in-Differences Estimates of 
**             Log Revenue-per-Call Changes During the Post-Intervention Period
*------------------------------------------------------------------------------*
	
*Internal control 
	reghdfe lrpc pairincentivepost structuredmeetingpost combinedpost     `persistencesample1' , absorb(manager Week) vce(cluster manager Week)	
	est store A
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,6] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,6] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,6] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,6] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
*External control 
	reghdfe lrpc   pairincentivepost structuredmeetingpost combinedpost    `persistencesample2' , absorb(manager Week) vce(cluster manager Week)	
	est store B
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,7] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,7] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,7] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,7] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
*Both internal and external control 
	reghdfe lrpc internalcontrolpost pairincentivepost structuredmeetingpost combinedpost     `persistencesample3'   , absorb(manager Week) vce(cluster manager Week)	
	est store C
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,8] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,8] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,8] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,8] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Balanced
	reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   `persistencesample4' , absorb(manager Week) vce(cluster manager Week)	
	est store D
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,10] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,10] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,10] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,10] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Both internal and external control individual fixed effects
	reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   `persistencesample5' , absorb(ID Week) vce(cluster manager Week) keepsingle	
	est store E
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,9] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,9] = round(r(p),0.001) 
		test pairincentivepost = combinedpost
		matrix pval[3,9] = round(r(p),0.001)  
		test combined   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,9] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Total revenue Both internal and external control individual fixed effects
	reghdfe revenue internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   `persistencesample5'  , absorb(manager Week) vce(cluster manager)	keepsingle
	est store F
	
	*Test similarity
		test structuredmeeting = pairincentive
		matrix pval[1,5] = round(r(p),0.001)  
		test structuredmeeting = combined
		matrix pval[2,5] = round(r(p),0.001) 
		test pairincentive = combined 
		matrix pval[3,5] = round(r(p),0.001)  
		test combined   - (pairincentive + structuredmeeting) = 0
		local sign_sum = sign(_b[combined] - (_b[pairincentive]+_b[structuredmeeting]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,5] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
 
 estout1 A B C D E F   using "$tablepath\TableA7.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(structuredmeetingpost  pairincentivepost  combinedpost internalcontrolpost) 


*------------------------------------------------------------------------------*
** Table A.8  Estimates of Persistence of Log RPC Gains Over Different 
**            Intervals in the Post-Intervention Period 
*------------------------------------------------------------------------------*

*Difference-in-differences indicator variables for post period	
	gen period1 = (Week >= 5 & Week <= 8)
	gen period2 = (Week >= 9 & Week <= 12)
	gen period3 = (Week >= 13 & Week <= 16)
	gen period4 = (Week >= 17 & Week <= 20)
	gen period5= (Week >= 21 & Week <= 24)
	local vars 
	local varsfull 

	forvalues i = 1(1)5 {
	
		gen pairincentivepost`i' = period`i'*(Treatment==2)
		gen structuredmeetingpost`i' = period`i'*(Treatment==3)
		gen combinedpost`i' = period`i'*(Treatment==4)
		local step`i'  structuredmeetingpost`i' pairincentivepost`i' combinedpost`i'  
		local vars `vars' `step`i''

		reghdfe lrpc  `step`i''      if (Week >= -3 & Week <= 0 & Treatment !=5 ) | (period`i'==1  & Treatment !=5 ) , absorb(manager Week) vce(cluster manager)
		est store A`i'
	
	}
	
 	reghdfe lrpc  `vars'   if (Week >= -3 & Week <= 0 & Treatment !=5 ) | (Week >= 5 & Week <= 24 & Treatment !=5 ) , absorb(manager Week) vce(cluster manager)
	est store A6

	estout1 A1 A2 A3 A4 A5 A6     using "$tablepath\TableA8.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace  

	drop pairincentivepost1 pairincentivepost2 pairincentivepost3 pairincentivepost4 pairincentivepost5 structuredmeetingpost1 structuredmeetingpost2 structuredmeetingpost3 structuredmeetingpost4 structuredmeetingpost5  combinedpost1 combinedpost2 combinedpost3 combinedpost4 combinedpost5  

*------------------------------------------------------------------------------*
** Table A.9 Estimates of Log Revenue-per-Call Changes During the Post-Intervention  
**           period weighting by the inverse number of weeks an agents is in the sample
*------------------------------------------------------------------------------*
 
*generate persistence sample and pnum
	gen persistsample = ((Week > -4 & Week < 1) | (Week > 4))
	bysort ID persistsample: gen num = _N  
	gen pnum = 1/num
  
*Internal control 
	reghdfe lrpc pairincentivepost structuredmeetingpost combinedpost   [pweight = pnum]  `persistencesample1' , absorb(manager Week) vce(cluster manager)	
	est store A
 
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,6] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,6] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,6] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,6] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*External control 
	reghdfe lrpc   pairincentivepost structuredmeetingpost combinedpost   [pweight = pnum]  `persistencesample2' , absorb(manager Week) vce(cluster manager)	
	est store B
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,7] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,7] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,7] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,7] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*Both internal and external control 
	reghdfe lrpc internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   [pweight = pnum]  `persistencesample3'   , absorb(manager Week) vce(cluster manager)	
	est store C
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,8] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,8] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,8] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,8] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*Balanced
	reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost  [pweight = pnum]  `persistencesample4' , absorb(manager Week) vce(cluster manager)	
	est store D
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,10] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,10] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,10] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,10] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Both internal and external control individual fixed effects
	reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost  [pweight = pnum] `persistencesample5' , absorb(ID Week) vce(cluster manager) keepsingle	
	est store E
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,9] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,9] = round(r(p),0.001) 
		test pairincentivepost = combinedpost
		matrix pval[3,9] = round(r(p),0.001)  
		test combined   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,9] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Total revenue Both internal and external control individual fixed effects
	reghdfe revenue internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   [pweight = pnum]  `persistencesample3'  , absorb(manager Week) vce(cluster manager)	keepsingle
	est store F
	
	*Test similarity
		test structuredmeeting = pairincentive
		matrix pval[1,5] = round(r(p),0.001)  
		test structuredmeeting = combined
		matrix pval[2,5] = round(r(p),0.001) 
		test pairincentive = combined 
		matrix pval[3,5] = round(r(p),0.001)  
		test combined   - (pairincentive + structuredmeeting) = 0
		local sign_sum = sign(_b[combined] - (_b[pairincentive]+_b[structuredmeeting]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,5] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 

 estout1 A B C D E F    using "$tablepath\TableA9.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(structuredmeetingpost  pairincentivepost  combinedpost internalcontrolpost) 		

*------------------------------------------------------------------------------*
** Table A.10   Agent Turnover at Different Horizons
**           
*------------------------------------------------------------------------------*

 preserve
 
 *add in updated term data
	merge m:1 ID using "$datapath/Termdataupdate", nogen

*generate termination variables
	gen term_week = round((termdate - td(17July2017))/7 + 1,1)
	gen termw8 = (term_week <=8)
	gen termw12 = (term_week <=12)
	gen termw16 = (term_week <=16)
	gen termw20 = (term_week <=20)
	gen termw24 = (term_week <=24)
	
*Term
	reg   termw8 t2 t3 t4   if   uniqueID ==1 & Treatment !=5 , vce(cluster manager)
	est store A
	
	reg  termw12 t2 t3 t4   if   uniqueID ==1 & Treatment !=5 , vce(cluster manager)
	est store B
	
	reg   termw16 t2 t3 t4   if   uniqueID ==1 & Treatment !=5 , vce(cluster manager)
	est store C
	
	reg   termw20 t2 t3 t4   if   uniqueID ==1 & Treatment !=5 , vce(cluster manager)
	est store D
	
	reg   termw24 t2 t3 t4   if   uniqueID ==1 & Treatment !=5 , vce(cluster manager)
	est store E
	
	estout1 A B C D E using  "$tablepath\TableA10.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace  keep(t3 t2 t4) 

	
	restore
 

*------------------------------------------------------------------------------*
** Table A.11  Treatment Effect heterogeneity on Log Revenue-per-Call    
**              by Partner and Agent Performance
*------------------------------------------------------------------------------*

* Note, see other code to get the wild bootstrap inference.

*generate independent variables in intervention and post period
	local independent_intervention  pairincentive structuredmeeting combined pairincentive_high structuredmeeting_high combined_high highperformerpartner highperformerpartner_ever 
	local independent_post pairincentivepost structuredmeetingpost combinedpost pairincentive_post_high structuredmeeting_post_high combined_post_high highperformerpartner hp_post 
	  
*Baseline estimate internal control
	reghdfe lrpc   `independent_intervention'  `highsample1' , absorb(manager Week) vce(cluster manager)	
	est store A
	
*Non-high performer person subsample
	reghdfe lrpc  `independent_intervention'  `highsample2' , absorb(manager Week) vce(cluster manager)	
	est store B
	
*High performer person subsample	
	reghdfe lrpc  `independent_intervention'  `highsample3', absorb(manager Week) vce(cluster manager)	
	est store C
	
*Post period
	reghdfe lrpc `independent_post'           `highsample4' , absorb(manager Week) vce(cluster manager)	
	est store D	
	
*Post period Non-high performer person subsample
	reghdfe lrpc      `independent_post'       `highsample5' , absorb(manager Week) vce(cluster manager)	
	est store E	
	
*Post period High performer person subsample
	reghdfe lrpc      `independent_post'        `highsample6' , absorb(manager Week) vce(cluster manager)	
	est store F	
	
estout1 A B C D  E F  using "$tablepath\TableA11.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep( structuredmeeting_high pairincentive_high combined_high structuredmeeting  pairincentive  combined  structuredmeeting_post_high pairincentive_post_high combined_post_high  structuredmeetingpost  pairincentivepost  combinedpost ) 
 

*------------------------------------------------------------------------------*
** Table A.12   Treatment Effect Heterogeneity on Total Calls
**               by Partner and Agent Performance   
*------------------------------------------------------------------------------*
* Note, see WildBootstrap code to get the wild bootstrap inference.

*generate independent variables in intervention and post period
	local independent_intervention  pairincentive structuredmeeting combined pairincentive_high structuredmeeting_high combined_high highperformerpartner highperformerpartner_ever 
	local independent_variables  
	  
	 foreach var in  `independent_intervention' {
		gen tbl_`var' = `var'
		local independent_variables `independent_variables' tbl_`var'
	 }
	  
	  
*Baseline estimate internal control
	reghdfe Calls_Week   `independent_variables'  `highsample1' , absorb(manager Week) vce(cluster manager)	
	est store A
	
*Non-high performer person subsample
	reghdfe Calls_Week  `independent_variables'  `highsample2' , absorb(manager Week) vce(cluster manager)	
	est store B
	
*High performer person subsample	
	reghdfe Calls_Week  `independent_variables'  `highsample3', absorb(manager Week) vce(cluster manager)	
	est store C
	

	 replace tbl_pairincentive = pairincentivepost
	 replace tbl_structuredmeeting = structuredmeetingpost
	 replace tbl_combined = combinedpost 
	 replace tbl_pairincentive_high = pairincentive_post_high
	 replace tbl_structuredmeeting_high = structuredmeeting_post_high
	 replace tbl_combined_high = combined_post_high
	 replace tbl_highperformerpartner = highperformerpartner
	 replace tbl_highperformerpartner_ever = hp_post 
	 
	 label var tbl_pairincentive "Pair-Incentives"
	 label var tbl_structuredmeeting "Structured-Meetings" 
	 label var tbl_combined  "Combined"
	 label var tbl_pairincentive_high "Pair-Incentives $\times$ High Performing Partner"  
	 label var tbl_structuredmeeting_high "Structured-Meetings $\times$ High Performing Partner" 
	 label var tbl_combined_high "Combined $\times$ High Performing Partner" 

	
	
*Post period
	reghdfe Calls_Week `independent_variables'           `highsample4' , absorb(manager Week) vce(cluster manager)	
	est store D	
	
*Post period Non-high performer person subsample
	reghdfe Calls_Week      `independent_variables'       `highsample5' , absorb(manager Week) vce(cluster manager)	
	est store E	
	
*Post period High performer person subsample
	reghdfe Calls_Week     `independent_variables'        `highsample6' , absorb(manager Week) vce(cluster manager)	
	est store F	
	
estout1 A B C D  E F  using "$tablepath\TableA12.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace ///
keep( tbl_structuredmeeting_high tbl_pairincentive_high tbl_combined_high tbl_structuredmeeting tbl_pairincentive tbl_combined ) 
 
 
*------------------------------------------------------------------------------*
** Table A.13  Example Worksheet Responses Organized by Word Prevalence 
**           
*------------------------------------------------------------------------------*

**Collected from worksheets



*------------------------------------------------------------------------------*
** Table A.14   Estimates of Log Revenue-per-Call Treatment Effects During 
**              the Post-Intervention Period, Allowing for Heterogeneous Effects 
**              if Partners Report to Different Managers
*------------------------------------------------------------------------------*


	gen proximity = 0 
	replace proximity = . if Week > 4  
	levelsof ID, local(IDlevels)
	foreach i of local IDlevels {
		forvalues t = 1(1)4 {
		sum Partner if Week == `t' & ID == `i'
		sum manager if ID == r(mean) & Week ==`t'
			if r(N) > 0 {
				scalar partnermanager = r(mean)
				sum manager if ID ==`i' & Week ==`t'
				replace proximity = 1 if r(mean) == `=partnermanager' & ID ==`i' & Week ==`t'
			}
		}
	}

	tab proximity  
	sum proximity if Week >0 & Week < 5
	sum proximity if Treatment ==1 & Week >0 & Week < 5
	sum proximity if Treatment ==2 & Week >0 & Week < 5
	sum proximity if Treatment ==3 & Week >0 & Week < 5
	sum proximity if Treatment ==4 & Week >0 & Week < 5

	bysort ID: egen proximity_ever = max(proximity)
	gen distant = (proximity ==0)
	replace distant = 0 if Week < 1
	bysort ID: egen distant_ever = max(distant)
	
	sum proximity_ever if Week >0 & Week < 5
	sum proximity_ever if Treatment ==1 & Week >0 & Week < 5
	sum proximity_ever if Treatment ==2 & Week >0 & Week < 5
	sum proximity_ever if Treatment ==3 & Week >0 & Week < 5
	sum proximity_ever if Treatment ==4 & Week >0 & Week < 5

	gen pairincentive_distant = pairincentive*distant
	gen structuredmeeting_distant = structuredmeeting*distant
	gen combined_distant = combined*distant
	gen internalcontrol_distant = internalcontrol*distant

	gen distant_post = 0
	replace distant_post = distant_ever if Week > 4
	
	gen proximity_post = 0 
	replace proximity_post = proximity_ever if Week > 4
	
	tab proximity_post if Week > 4

	gen pairincentive_distant_post = pairincentivepost*distant_post
	gen structuredmeeting_distant_post = structuredmeetingpost*distant_post
	gen combined_distant_post = combinedpost*distant_post
	gen internalcontrol_distant_post = internalcontrolpost*distant_post
	  
	*Log revenue per call
	**Internal control 
		reghdfe lrpc pairincentivepost structuredmeetingpost combinedpost   pairincentive_distant_post   structuredmeeting_distant_post   combined_distant_post  distant_ever    `persistencesample1' , absorb(manager Week) vce(cluster manager)	
		est store A

	**External control 
		reghdfe lrpc   pairincentivepost structuredmeetingpost combinedpost  pairincentive_distant_post   structuredmeeting_distant_post   combined_distant_post  distant_ever   `persistencesample2' , absorb(manager Week) vce(cluster manager)	
		est store B
	 
	**Both internal and external control 
		reghdfe lrpc internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   internalcontrol_distant_post pairincentive_distant_post   structuredmeeting_distant_post   combined_distant_post  distant_ever   `persistencesample3'   , absorb(manager Week) vce(cluster manager)	
		est store C
	 
	**Balanced
		reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   internalcontrol_distant_post pairincentive_distant_post   structuredmeeting_distant_post   combined_distant_post  distant_ever `persistencesample4' , absorb(manager Week) vce(cluster manager)	
		est store D

	**Both internal and external control individual fixed effects
		reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   internalcontrol_distant_post pairincentive_distant_post   structuredmeeting_distant_post   combined_distant_post  distant_ever  `persistencesample5' , absorb(ID Week) vce(cluster manager) keepsingle	
		est store E
		
**Total revenue Both internal and external control individual fixed effects
	reghdfe revenue internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   internalcontrol_distant_post pairincentive_distant_post   structuredmeeting_distant_post   combined_distant_post  distant_ever   `persistencesample5' , absorb(manager Week) vce(cluster manager)	keepsingle
	est store F

	 estout1 A B C D E F   using "$tablepath\TableA14.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace ///
	 keep(structuredmeetingpost  pairincentivepost  combinedpost internalcontrolpost structuredmeeting_distant_post  pairincentive_distant_post      combined_distant_post  ) 


*------------------------------------------------------------------------------*
** Table A.15   Estimates of Log Revenue-per-Call Treatment Effect Heterogeneity  
**              by Rotation and Baseline Connections
*------------------------------------------------------------------------------*

	preserve

	merge 1:1 ID Week using "$datapath\survey1.dta"


	bysort ID: egen temp = mode(Repair1)
	replace Repair1 = temp if Repair1 ==.
	replace Repair1 =0 if Repair1 ==.
	drop temp

	bysort ID: egen temp = mode(connected)
	replace connected = temp if connected ==.

	capture drop connectedFlag
	 gen connectedFlag = (connected ==.)
	replace connected = 0 if connectedFlag ==1

	drop if Treatment==5


	gen TD4 = Treatment==1
	gen TD1 = Treatment==3
	label var TD1 "Structured Meetings"
	gen TD2 = Treatment==2
	label var TD2 "Pair Incentives"
	gen TD3 = Treatment==4
	label var TD3 "Combined"

	forvalues i = 1/3 {
	gen PTD_`i' = (Week>=1)*TD`i'
	foreach j in connected Repair1 connectedFlag {
	  gen PTD_`i'_`j'=PTD_`i'*`j'
	}
	}

	foreach j in connected Repair1 connectedFlag {
	  gen Post_`j' = (Week>0)*`j'
	}

	label var PTD_1_connected "Structured-Meetings x Connected"
	label var PTD_2_connected "Pair-Incentives x Connected"
	label var PTD_3_connected "Combined x Connected"

	label var PTD_1_Repair1 "Structured-Meetings x Rotate Pairs"
	label var PTD_2_Repair1 "Pair-Incentives x Rotate Pairs"
	label var PTD_3_Repair1 "Combined x Rotate Pairs"

	label var PTD_1 "Structured-Meetings"
	label var PTD_2 "Pair-Incentives"
	label var PTD_3 "Combined"


	reghdfe lrpc PTD_1 PTD_2 PTD_3 Post_Repair1 PTD_1_Repair1  PTD_2_Repair1  ///
		PTD_3_Repair1 `baselinesample1', absorb(manager Week) vce(cluster manager Week)	
		est store A

	reghdfe lrpc PTD_1 PTD_2 PTD_3 Post_Repair1 PTD_1_Repair1  PTD_2_Repair1  ///
		PTD_3_Repair1 `persistencesample1' & highperformerpartner_ever==0, absorb(manager Week) vce(cluster manager Week)	
		est store B

	reghdfe lrpc PTD_1 PTD_2 PTD_3 Post_Repair1 PTD_1_Repair1  PTD_2_Repair1  ///
		PTD_3_Repair1 `persistencesample1' & highperformerpartner_ever==1, absorb(manager Week) vce(cluster manager Week)	
		est store C

		
	reghdfe lrpc PTD_1 PTD_2 PTD_3 Post_connected ///
		PTD_1_connected PTD_2_connected ///
		PTD_3_connected `baselinesample1' & ~ connectedFlag, absorb(manager Week) vce(cluster manager Week)	
		est store D

	reghdfe lrpc PTD_1 PTD_2 PTD_3 Post_connected ///
		PTD_1_connected PTD_2_connected ///
		PTD_3_connected `persistencesample1' & ~ connectedFlag, absorb(manager Week) vce(cluster manager Week)	
		est store E


	estout1 A B C D E using "Tables\TableA15.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) ///
		label cons(Constant) replace keep( PTD_1_Repair1 PTD_2_Repair1 PTD_3_Repair1 PTD_1_connected PTD_2_connected PTD_3_connected PTD_1 PTD_2 PTD_3)

	restore

*------------------------------------------------------------------------------*
** Table A.16   Estimates of Treatment Effects on Log Revenue-per-Call During 
**              the Post-Intervention Period for Agents in the Top 75\% 
**              of the Tenure Distribution
*------------------------------------------------------------------------------*
 
*Internal control 
	reghdfe lrpc pairincentivepost structuredmeetingpost combinedpost     `checkpersistsample1' , absorb(manager Week) vce(cluster manager)	
	est store A
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,6] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,6] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,6] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,6] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*External control 
	reghdfe lrpc   pairincentivepost structuredmeetingpost combinedpost    `checkpersistsample2' , absorb(manager Week) vce(cluster manager)	
	est store B
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,7] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,7] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,7] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,7] = round(normal(`sign_sum'*sqrt(r(F))),0.001)  
		
*Both internal and external control 
	reghdfe lrpc internalcontrolpost pairincentivepost structuredmeetingpost combinedpost     `checkpersistsample3'   , absorb(manager Week) vce(cluster manager)	
	est store C
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,8] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,8] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,8] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,8] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Balanced
	reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   `checkpersistsample4' , absorb(manager Week) vce(cluster manager)	
	est store D
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,10] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,10] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,10] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,10] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Both internal and external control individual fixed effects
	reghdfe lrpc   internalcontrolpost pairincentivepost structuredmeetingpost combinedpost   `checkpersistsample5' , absorb(ID Week) vce(cluster manager) keepsingle	
	est store E
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,9] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,9] = round(r(p),0.001) 
		test pairincentivepost = combinedpost
		matrix pval[3,9] = round(r(p),0.001)  
		test combined   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,9] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 
		
*Revenue
	reghdfe revenue  internalcontrolpost pairincentivepost structuredmeetingpost combinedpost     `checkpersistsample3'   , absorb(manager Week) vce(cluster manager)	
	est store F
	
	*Test similarity
		test structuredmeetingpost = pairincentivepost
		matrix pval[1,10] = round(r(p),0.001)  
		test structuredmeetingpost = combinedpost
		matrix pval[2,10] = round(r(p),0.001) 
		test pairincentivepost = combinedpost 
		matrix pval[3,10] = round(r(p),0.001)  
		test combinedpost   - (pairincentivepost + structuredmeetingpost) = 0
		local sign_sum = sign(_b[combinedpost] - (_b[pairincentivepost]+_b[structuredmeetingpost]))
		display "H_0: combined coef >= sum coef. p-value = " normal(`sign_sum'*sqrt(r(F)))
		matrix pval[4,10] = round(normal(`sign_sum'*sqrt(r(F))),0.001) 


 estout1 A B C D E F   using "$tablepath\TableA16.tex", style(tex) se(par) stats(r2_a N) nohead stlabels("Adj. R-Squared" "Observations") stfmt(%9.3f %9.0fc %9.0fc) star(0.10 0.05 0.01) label cons(Constant) replace keep(structuredmeetingpost  pairincentivepost  combinedpost internalcontrolpost) 

	
 
*------------------------------------------------------------------------------*
* Figure A.1:  Perceived and Actual Differences Between Individual and the Top
*              Sales Agents
*------------------------------------------------------------------------------* 
 
 preserve
 
 insheet using "$datapath\1810-survey-roster.csv", clear
	rename RepGpId ID
	keep ID location team group 
	sort ID
	tempfile repdata
save "`repdata'"

insheet using "$datapath\Helping Survey_October 19, 2018_13.20.csv", clear
	drop if _n<5
	rename v14 lat
	replace lat = substr(lat,1,6)
	drop if strpos(v29,"not a sales rep")>0
	drop if strpos(v29,"I am in a support role")>0
	rename v19 ID
	rename v22 ownRPC
	label var ownRPC "Agent's Reported RPC"
	rename v23 ownRPCRank
	rename v26 thirdHighest
	rename v27 highestRPC
	rename v28 whyHigherRPC

	rename v44 howMuchDoBestHelp
	label var howMuchDoBestHelp "Agent Estimate of Sales Percent Change After Help from Best Agent"
	
	drop v*
	destring howMuchDoBestHelp ownRPC, replace force

	destring ID, replace force
	drop if ID==.
	sort ID
	merge ID using "`repdata'"
	tab _merge
	keep if _merge==3
	drop _merge
	sort ID

* Estimate of how much asking best agent to help would matter
	sum howMuchDoBestHelp, d
	scalar define rmn = round(r(mean),2)
	scalar define rmed = round(r(p50),2)
	
	sort ID ownRPC highestRPC
	by ID: gen dup = cond(_N==1,0,_n)
	drop if dup > 1
	tempfile survey
	save "`survey'"
	

	use "$datapath\DataforAnalysis.dta", clear
	sort ID
	merge ID using "`survey'"
	tab _merge
	destring ownRPC highestRPC, replace

	bysort ID: egen avgRPC = mean(RPC) if _N>1

	
	egen LocationNum = group(location)

	drop if location==3
	bysort brand LocationNum: egen maxAvg = max(avgRPC)

	sort ID avgRPC location brand
	by ID: keep if _n==1
	gen hasAvgRPC = avgRPC<.

	gen perceivedDist = (highestRPC-ownRPC)
	gen actualDist = (maxAvg-avgRPC) if hasAvgRPC

	binscatter actualDist perceivedDist if hasAvgRPC & perceivedDist>=0, ///
	xtitle("Perceived Maximum RPC - Perceived Own RPC") ytitle("Actual Maximum RPC - Actual Own RPC")
	graph export "$figurepath\FigurA1.pdf", replace	
	

restore
*------------------------------------------------------------------------------*
*  Figure A.2:  Revenue-per-call Over an Extended Post-intervention Period
*------------------------------------------------------------------------------*
	*reset variables
		cap drop graphRPC
		cap drop WeekTreat
 
	*generate variable week by treatment
		gen WeekTreat = .
		forvalues i = 1(1)5 { 
			replace WeekTreat = Week + 8 + (`i' - 1)*42 if Treatment == `i'
		}
	*create variable that is the average revenue per call for week by treatment
		bysort WeekTreat: egen graphRPC = mean(RPC)	

		
	*graph 
	#delimit ;
		twoway connected graphRPC Week if Treatment == 1   , ms(Sh) lp(dash) lc(gs8)  lw(medthick) || 
		connected graphRPC Week if Treatment == 2    , ms(d) mc(gs4) lc(gs4) lw(medthick) || 
		connected graphRPC Week if Treatment == 3    , ms(T) mc(gs6) lc(gs6) lw(medthick)|| 
		connected graphRPC Week if Treatment == 4 , lc(black) mc(black) lw(medthick)  || 
		line graphRPC Week if Treatment == 5  ,  lp(dash) lc(gs10) lw(medthick)   
		legend(lab(1 "Internal Control") lab(2 "Pair-Incentives") lab(3 "Structured-Meetings") lab(4 "Combined") lab(5 "External Control") order(3 2 4 1 5) region(color(white)) ) 
		xlab(-6(2)24) xscale(r(-7 24)) 
		graphregion(color(white)) ytitle("Revenue-per-Call (Normalized)") 
		xline(0, lc(black)) xline(4, lc(black)) ;
		graph export "$figurepath\FigureA2.pdf", replace;

	#delimit cr 	
	
	
*------------------------------------------------------------------------------*
* Figure A.3: Average Revene-per-Call in the Intervention 
*           and Post-Intervention Periods by High-Performer Partner Assigment 
*------------------------------------------------------------------------------*	
 
	*reorder the bars external control first
		cap drop graph_treat 
		gen graph_treat = Treatment  
		replace graph_treat = . if Treatment ==5
	*varibles before and during treatment
		gen rpc_treat_lowpartner = .
		gen rpc_treat_highpartner = .
		gen rpc_post_lowpartner = .
		gen rpc_post_highpartner = .

	*fill in graph variables
		forvalues i = 1(1) 5 {
			sum RPC if Treatment ==`i' & period1_4 ==1 & highperformerpartner ==0 
			replace rpc_treat_lowpartner = r(mean) if Treatment ==`i' & period1_4 ==1 & highperformerpartner ==0
			sum RPC if Treatment ==`i' & period1_4 ==1 & highperformerpartner ==1 
			replace rpc_treat_highpartner = r(mean) if Treatment ==`i' & period1_4 ==1 & highperformerpartner ==1 

			
			sum RPC if Treatment ==`i' & Week > 4 & highperformerpartner ==0 
			replace rpc_post_lowpartner = r(mean) if Treatment ==`i' & Week > 4 & highperformerpartner ==0
			sum RPC if Treatment ==`i' & Week > 4 & highperformerpartner ==1 
			replace rpc_post_highpartner = r(mean) if Treatment ==`i' & Week > 4 & highperformerpartner ==1 
		
		}
		
	*graph  
		#delimit ; 
			graph bar  rpc_treat_lowpartner rpc_treat_highpartner rpc_post_lowpartner rpc_post_highpartner, over(graph_treat,  
			relabel(1 "Internal Control" 2 "Pair-Incentives"   
			 3 "Structured-Meetings" 4 "Combined"   )  )   
			graphregion(color(white))   ytitle("Revenue-per-Call")  
			legend(lab(1 "Intervention Period, Low-Performer Partner") lab(2 "Intervention Period, High-Performer Partner") lab(3 "Post-Intervention, Low-Performer Partner") lab(4 			
			"Post-Intervention, High-Performer Partner") col(1) region(color(white))) 
			bar(1, color(gs12) fintensity(inten40))
			bar(2, color(gs2) fintensity(inten40))
			bar(3, color(gs12) fintensity(inten80))
			bar(4, color(gs2) fintensity(inten80))
			scale(0.9);
			graph export "$figurepath\FigureA3.pdf", replace;
		#delimit cr 
		
		drop rpc_treat_lowpartner rpc_treat_highpartner rpc_post_lowpartner rpc_post_highpartner	
	



*------------------------------------------------------------------------------*
* Figure A.4:  Agents' Reported Estimates of Perceived Treatment Effects After
*              Help from High-Performers
*------------------------------------------------------------------------------* 
	use "`survey'", clear
	histogram howMuchDoBestHelp, freq bin(10) note(Mean is `=rmn' and Median is `=rmed'.) graphregion(fcolor(gs16))
	graph export "$figurepath\FigureA4.pdf", replace	
	
	
*------------------------------------------------------------------------------*
* Figure A.5:  Knowledge Transfer Region
*               
*------------------------------------------------------------------------------* 
 	
clear
set more off

set obs 10000

*initial threshold
	scalar k = .075
	
*initial commission
	scalar b = .425
	
*threshold with meetings
	scalar kmeet = .035
 
*commission with bonus
	scalar bbonus = .05  // This is the incremental with bonus.

*knowledge of knowledge seeker
	gen thetai = _n/10000

*threshold level of knowledge provider
	gen thetaj = min(sqrt(2*k/b^2 + thetai^2),1)

*threshold level of knowledge provider
	gen thetajmeet = min(sqrt(2*kmeet/b^2 + thetai^2),1)
	
*threshold level of knowledge provider
	gen thetajbonus = min(sqrt(2*k/((bbonus+b)^2+bbonus^2) + thetai^2),1)
	
*threshold level of knowledge provider
	gen thetajcombined = min(sqrt(2*kmeet/((bbonus+b)^2+bbonus^2) + thetai^2),1)
	
	gen upper = 1

#delimit ;
	twoway rarea upper thetajcombined thetai, color(gs15) ||
	line thetaj thetai, lw(medthick) lc(black) ||
	line thetajbonus thetai, lw(medthick) lc(navy) lp(longdash) ||
	line thetajmeet thetai, lw(medthick) lc(cranberry) lp(shortdash) || 
	line thetajcombined thetai, lw(medthick) lc(dkgreen ) lp(longdash_dot)
	text(3 4.5 "Knowledge Transfer Region", color(black))
	graphregion(color(white))
	legend(order(2 3 4 5 1) lab(2 "Internal Control threshold") lab(3 "Pair-Incentives threshold") lab(4 "Structured-Meetings threshold") lab(5 "Combined threshold") lab(1 "Knowledge transfer region")region(lcolor(white)))
	ytitle("Knowledge of Provider {&theta}{sub:j}")
	xtitle("Knowledge of Seeker {&theta}{sub:i}");
	graph export "$figurepath\FigureA5.eps", replace;
	
#delimit cr	
	
log close
